//
//  DebugMainViewController.swift
//  YKSwiftDebugManager
//
//  Created by linghit on 2021/12/2.
//

import UIKit

internal class DebugMainViewController: DebugBaseViewController,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
    
    private lazy var collectionView:UICollectionView = {
        let layout = DebugMainViewLayout.init()
        let cView = UICollectionView.init(frame: CGRect(x: 0, y: top_Height(), width: self.view.bounds.size.width, height: self.view.bounds.size.height - top_Height() - bottom_safe()), collectionViewLayout: layout)
        cView.backgroundColor = .clear
        cView.layer.borderColor = UIColor.clear.cgColor
        cView.showsVerticalScrollIndicator = false
        cView.showsHorizontalScrollIndicator = false
        cView.isScrollEnabled = true
        cView.delegate = self
        cView.dataSource = self
        cView.register(DebugMainItemCell.classForCoder(), forCellWithReuseIdentifier: "DebugMainItemCell")
        cView.register(DebugMainSwitchCell.classForCoder(), forCellWithReuseIdentifier: "DebugMainSwitchCell")
        cView.register(UICollectionReusableView.classForCoder(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "UICollectionReusableView")
        return cView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "主页面"
        // Do any additional setup after loading the view.
        view.addSubview(collectionView)
        self.setNeedsStatusBarAppearanceUpdate()
        NotificationCenter.default.addObserver(self, selector: #selector(getNotification(notification:)), name: NSNotification.Name.init(rawValue: DebugKeyValue.kSandBoxViewShow), object: nil)
    }
    
    @objc func getNotification(notification:Notification)->Void{
        if notification.name == NSNotification.Name.init(rawValue: DebugKeyValue.kSandBoxViewShow) {
            
        }
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return DebugManagerUtil.share.numberOfSections()
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return DebugManagerUtil.share.numberOfItemsInSection(section: section)
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let itemName = DebugManagerUtil.share.cellForItem(indexPath: indexPath)
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: itemName, for: indexPath) as! DebugBaseCollectionViewCell
        cell.detail(indexPath: indexPath)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        DebugManagerUtil.share.itemClick(indexPath: indexPath, controller: self)
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "UICollectionReusableView", for: indexPath)
        view.backgroundColor = .lightGray
        let titlLabel = UILabel.init(frame: CGRect(x: 15, y: 0, width: 100, height: view.bounds.size.height))
        titlLabel.font = UIFont.systemFont(ofSize: 12, weight: .bold)
        titlLabel.text = DebugManagerUtil.share.nameOfSections(indexPath: indexPath)
        if #available(iOS 13.0, *) {
            titlLabel.textColor = .label
        } else {
            titlLabel.textColor = .black
        }
        view.addSubview(titlLabel)
        return view
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.size.width, height: 25)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
}
